package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.equipment.EquipmentBook;
import com.ruoyi.system.domain.equipment.EquipmentByzx;
import com.ruoyi.system.domain.equipment.EquipmentByzxDTO;
import com.ruoyi.system.domain.equipment.EquipmentByzxHistory;
import com.ruoyi.system.mapper.equipment.EquipmentBookMapper;
import com.ruoyi.system.mapper.equipment.EquipmentByzxHistoryMapper;
import com.ruoyi.system.mapper.equipment.EquipmentByzxMapper;
import com.ruoyi.system.mapper.equipment.EquipmentTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.IEquipmentByzxService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 设备保养执行Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-13
 */
@Service
public class EquipmentByzxServiceImpl extends ServiceImpl<EquipmentByzxMapper, EquipmentByzx> implements IEquipmentByzxService {
    @Autowired
    private EquipmentBookMapper equipmentBookMapper;
    @Autowired
    private EquipmentByzxHistoryMapper equipmentByzxHistoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult byzx(EquipmentByzxDTO dto) {
        EquipmentByzx equipmentByzx = new EquipmentByzx();
        BeanUtil.copyProperties(dto, equipmentByzx);
        String join = StringUtils.join(dto.getImgPath(), ",");
        equipmentByzx.setImgPath(join);

        equipmentByzx.setUpdateBy(SecurityUtils.getUsername());
        equipmentByzx.setUpdateTime(new Date());
//        equipmentByzx.setByDate(new Date());前端传

        //根据设备id获取设备名称
        EquipmentBook equipmentBook = equipmentBookMapper.selectById(dto.getEquipmentId());
        equipmentByzx.setEquipmentCode(equipmentBook.getBookCode());
        equipmentByzx.setEquipmentName(equipmentBook.getBookName());
        //计算下次保养执行时间
        Integer totalPower = equipmentBook.getTotalPower();
        DateTime offsetDate = DateUtil.offset(equipmentByzx.getByDate(), DateField.MONTH, totalPower);
        equipmentByzx.setNextBydate(offsetDate);
        //状态变为不预警
        equipmentByzx.setAlertState(0);
        baseMapper.updateById(equipmentByzx);

        //同步更新设备台账的校准时间
        equipmentBook.setApplyDate(offsetDate);
        equipmentBookMapper.updateById(equipmentBook);

        //同时生成设备执行历史记录
        EquipmentByzxHistory equipmentByzxHistory = new EquipmentByzxHistory();
        BeanUtil.copyProperties(equipmentByzx, equipmentByzxHistory);
        equipmentByzxHistoryMapper.insert(equipmentByzxHistory);
        return AjaxResult.success("执行成功！");
    }
}
